package com.cg.system.interceptor;

import com.cg.busniess.entity.UserInfo;
import com.cg.system.util.ResourceUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by SimonZou on 16/3/24.
 */
public class LoginInterceptor implements HandlerInterceptor {
    private List<String> excludUrls;
    private String redirectUrl;
    private List<String> excludeContainUrls;

    public List<String> getExcludeContainUrls() {
        return excludeContainUrls;
    }

    public void setExcludeContainUrls(List<String> excludeContainUrls) {
        this.excludeContainUrls = excludeContainUrls;
    }

    public List<String> getExcludUrls() {
        return excludUrls;
    }

    public void setExcludUrls(List<String> excludUrls) {
        this.excludUrls = excludUrls;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute("userInfo");
        System.err.println(request.getContextPath());
        String requestPath = ResourceUtil.getRequestPath(request);
        if(this.excludeContainUrls!=null){
            if(this.moHuContain(this.excludeContainUrls,requestPath)){
                return true;
            }
        }
        if (this.excludUrls.contains(requestPath)) {
            return true;
        }
        if (user != null) {
            return true;
        } else {
            this.foward(request, response);
            return false;
        }

    }

    private void foward(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String path = request.getContextPath();
        String basePath = scheme + "://" + serverName + ":" + port + path;
        System.err.println(basePath + "/" + redirectUrl);
        response.sendRedirect(basePath + redirectUrl);

    }

    /**
     * 模糊匹配字符串
     * @param list
     * @param key
     * @return
     */
    private boolean moHuContain(List<String> list,String key){
        for(String str : list){
            if(key.contains(str)){
                return true;
            }
        }
        return false;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
